package com.bestcem.xm.ticket.controller.convert;

import com.bestcem.xm.ticket.controller.vo.ticket.config.*;
import com.bestcem.xm.ticket.dao.dto.TicketConfigInfo;
import com.bestcem.xm.ticket.dao.dto.TicketConfigV3Info;
import com.bestcem.xm.ticket.entity.mongo.*;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author XiaoYunSong <ys.xiao@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/8 15:51
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TicketConfigVOConvert {

    @Nullable
    public static TicketConfigInfoVO entityToTicketConfigInfoVO(TicketConfigInfo entity) {
        if (Objects.isNull(entity)) {
            return null;
        }
        TicketConfigInfoVO data = new TicketConfigInfoVO();

        data.setId(entity.getId());
        data.setRecordFlag(entity.getRecordFlag());
        data.setCreatedId(entity.getCreatedId());
        data.setCreatedDate(entity.getCreatedDate());
        data.setUpdatedId(entity.getUpdatedId());
        data.setUpdatedDate(entity.getUpdatedDate());

        data.setSysId(entity.getSysId());
        data.setOrgId(entity.getOrgId());
        data.setUserId(entity.getUserId());
        data.setSurveyId(entity.getSurveyId());
        data.setTicketFlowIds(entity.getTicketFlowIds());
        data.setCreateTrigger(entity.getCreateTrigger());
        data.setAlertId(entity.getAlertId());
        data.setAlertRuleIds(entity.getAlertRuleIds());
        data.setAlertRuleCount(entity.getAlertRuleCount());
        data.setOnlyHasContact(entity.getOnlyHasContact());
        data.setRuleList(entity.getRuleList());
        data.setTitle(entity.getTitle());
        data.setState(entity.getState());
        data.setTicketRuleId(entity.getTicketRuleId());
        data.setNeedDeal(entity.getNeedDeal());
        data.setNeedRecord(entity.getNeedRecord());
        data.setRemindAdmin(entity.getRemindAdmin());
        data.setSmtpId(entity.getSmtpId());
        data.setTicketTemplateId(entity.getTicketTemplateId());
        data.setTicketNotifyConfigIds(entity.getTicketNotifyConfigIds());
        data.setStatusRuleJsonList(entity.getStatusRuleList());

        List<TemplateDetailFieldVO> templateDetailFieldVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getTemplateDetailFields())) {
            List<Ticket.TemplateDetailField> templateDetailFields = entity.getTemplateDetailFields();
            for (Ticket.TemplateDetailField templateDetailField : templateDetailFields) {
                templateDetailFieldVOList.add(TemplateDetailFieldVO.getInstance(templateDetailField));
            }
        }
        data.setTemplateDetailFields(templateDetailFieldVOList);

        List<TicketConfigFlowVO> ticketConfigFlowVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getTicketFlow())) {
            List<TicketFlow> ticketFlows = entity.getTicketFlow();
            for (TicketFlow ticketFlow : ticketFlows) {
                ticketConfigFlowVOS.add(entityToTicketConfigFlowVO(ticketFlow));
            }
        }
        data.setTicketFlows(ticketConfigFlowVOS);
//       2,3 版本去除连表查询工单处理模板，在工单配置中设置有处理模板id，ticketTemplateId

        List<TicketConfigRuleVO> ticketConfigRuleVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getTicketTemplate())) {
            List<TicketRule> ticketRules = entity.getTicketRule();
            for (TicketRule ticketRule : ticketRules) {
                ticketConfigRuleVOList.add(entityToTicketConfigRuleVO(ticketRule));
            }
        }
        data.setTicketRules(ticketConfigRuleVOList);

        List<TicketNotifyConfigVO> ticketNotifyConfigVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getTicketNotifyConfig())) {
            List<TicketNotifyConfig> ticketNotifyConfigs = entity.getTicketNotifyConfig();
            for (TicketNotifyConfig ticketNotifyConfig : ticketNotifyConfigs) {
                ticketNotifyConfigVOList.add(entityToTicketNotifyConfigVO(ticketNotifyConfig));
            }
        }
        data.setTicketNotifyConfigS(ticketNotifyConfigVOList);

        return data;
    }

    @Nullable
    public static TicketConfigInfoV3VO entityToTicketConfigInfoVO(TicketConfigV3Info entity) {
        if (Objects.isNull(entity)) {
            return null;
        }
        TicketConfigInfoV3VO data = new TicketConfigInfoV3VO();

        data.setId(entity.getId());
        data.setRecordFlag(entity.getRecordFlag());
        data.setCreatedId(entity.getCreatedId());
        data.setCreatedDate(entity.getCreatedDate());
        data.setUpdatedId(entity.getUpdatedId());
        data.setUpdatedDate(entity.getUpdatedDate());

        data.setSysId(entity.getSysId());
        data.setOrgId(entity.getOrgId());
        data.setUserId(entity.getUserId());
        data.setSurveyId(entity.getSurveyId());
        data.setTicketFlowIds(entity.getTicketFlowIds());
        data.setCreateTrigger(entity.getCreateTrigger());
        data.setAlertId(entity.getAlertId());
        data.setAlertRuleIds(entity.getAlertRuleIds());
        data.setAlertRuleCount(entity.getAlertRuleCount());
        data.setOnlyHasContact(entity.getOnlyHasContact());
        data.setRuleList(entity.getRuleList());
        data.setTitle(entity.getTitle());
        data.setState(entity.getState());
        data.setTicketRuleId(entity.getTicketRuleId());
        data.setNeedDeal(entity.getNeedDeal());
        data.setNeedRecord(entity.getNeedRecord());
        data.setRemindAdmin(entity.getRemindAdmin());
        data.setSmtpId(entity.getSmtpId());
        data.setTicketTemplateId(entity.getTicketTemplateId());
        data.setTicketNotifyConfigIds(entity.getTicketNotifyConfigIds());
        data.setStatusRuleJsonList(entity.getStatusRuleList());

        List<TemplateDetailFieldVO> templateDetailFieldVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getTemplateDetailFields())) {
            List<Ticket.TemplateDetailField> templateDetailFields = entity.getTemplateDetailFields();
            for (Ticket.TemplateDetailField templateDetailField : templateDetailFields) {
                templateDetailFieldVOList.add(TemplateDetailFieldVO.getInstance(templateDetailField));
            }
        }
        data.setTemplateDetailFields(templateDetailFieldVOList);

        List<TicketConfigFlowVO> ticketConfigFlowVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getTicketFlow())) {
            List<TicketFlow> ticketFlows = entity.getTicketFlow();
            for (TicketFlow ticketFlow : ticketFlows) {
                ticketConfigFlowVOS.add(entityToTicketConfigFlowVO(ticketFlow));
            }
        }
        data.setTicketFlows(ticketConfigFlowVOS);

        List<TicketConfigTemplateV2VO> ticketConfigTemplateVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getTicketTemplate())) {
            List<TicketTemplateV2> ticketTemplates = entity.getTicketTemplate();
            for (TicketTemplateV2 ticketTemplate : ticketTemplates) {
                // 只设置id与name
                TicketConfigTemplateV2VO ticketConfigTemplateV2VO = new TicketConfigTemplateV2VO();
                ticketConfigTemplateV2VO.setId(ticketTemplate.getId());
                ticketConfigTemplateV2VO.setName(ticketTemplate.getName());
                ticketConfigTemplateVOList.add(ticketConfigTemplateV2VO);
            }
        }
        data.setTicketTemplates(ticketConfigTemplateVOList);

        List<TicketConfigRuleVO> ticketConfigRuleVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getTicketTemplate())) {
            List<TicketRule> ticketRules = entity.getTicketRule();
            for (TicketRule ticketRule : ticketRules) {
                ticketConfigRuleVOList.add(entityToTicketConfigRuleVO(ticketRule));
            }
        }
        data.setTicketRules(ticketConfigRuleVOList);

        List<TicketNotifyConfigVO> ticketNotifyConfigVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getTicketNotifyConfig())) {
            List<TicketNotifyConfig> ticketNotifyConfigs = entity.getTicketNotifyConfig();
            for (TicketNotifyConfig ticketNotifyConfig : ticketNotifyConfigs) {
                ticketNotifyConfigVOList.add(entityToTicketNotifyConfigVO(ticketNotifyConfig));
            }
        }
        data.setTicketNotifyConfigS(ticketNotifyConfigVOList);

        return data;
    }

    @Nullable
    public static TicketConfigFlowVO entityToTicketConfigFlowVO(TicketFlow entity) {
        if (Objects.isNull(entity)) {
            return null;
        }
        TicketConfigFlowVO data = new TicketConfigFlowVO();
        data.setId(entity.getId());
        data.setRecordFlag(entity.getRecordFlag());
        data.setCreatedDate(entity.getCreatedDate());
        data.setCreatedId(entity.getCreatedId());
        data.setUpdatedDate(entity.getUpdatedDate());
        data.setUpdatedId(entity.getUpdatedId());

        data.setOrgId(entity.getOrgId());
        data.setUserId(entity.getUserId());
        data.setTicketConfigId(entity.getTicketConfigId());
        data.setFlowId(entity.getFlowId());
        data.setTitle(entity.getTitle());
        data.setStatus(entity.getStatus());
        data.setMatchCondition(entity.getMatchCondition());
        data.setAlertRuleIds(entity.getAlertRuleIds());
        data.setPriority(entity.getPriority());
        data.setNeedAudit(entity.getNeedAudit());

        List<TicketConfigFlowVO.CustomRuleId> customRuleIdVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getCustomRuleIds())) {
            List<TicketFlow.CustomRuleId> customRuleIds = entity.getCustomRuleIds();
            for (TicketFlow.CustomRuleId customRuleId : customRuleIds) {
                customRuleIdVoList.add(TicketConfigFlowVO.CustomRuleId.getInstance(customRuleId));
            }
        }
        data.setCustomRuleIds(customRuleIdVoList);

        List<TicketConfigFlowVO.FollowConfig> followConfigVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getFollowConfig())) {
            List<TicketFlow.FollowConfig> followConfig = entity.getFollowConfig();
            for (TicketFlow.FollowConfig config : followConfig) {
                followConfigVoList.add(TicketConfigFlowVO.FollowConfig.getInstance(config));
            }
        }
        data.setFollowConfigs(followConfigVoList);

        List<TicketConfigFlowVO.FollowConfig> auditConfigVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getAuditConfig())) {
            List<TicketFlow.FollowConfig> auditConfig = entity.getAuditConfig();
            for (TicketFlow.FollowConfig config : auditConfig) {
                auditConfigVoList.add(TicketConfigFlowVO.FollowConfig.getInstance(config));
            }
        }
        data.setAuditConfigs(auditConfigVoList);

        data.setVersion(entity.getVersion());
        data.setRuleList(entity.getRuleList());

        return data;
    }

    @Nullable
    public static TicketConfigTemplateVO entityToTicketConfigTemplateVO(TicketTemplate entity) {
        if (Objects.isNull(entity)) {
            return null;
        }
        TicketConfigTemplateVO data = new TicketConfigTemplateVO();
        data.setId(entity.getId());
        data.setRecordFlag(entity.getRecordFlag());
        data.setCreatedId(entity.getCreatedId());
        data.setCreatedDate(entity.getCreatedDate());
        data.setUpdatedId(entity.getUpdatedId());
        data.setUpdatedDate(entity.getUpdatedDate());

        data.setOrgId(entity.getOrgId());
        data.setTicketConfigId(entity.getTicketConfigId());
        data.setCategory(entity.getCategory());
        data.setCustomFieldLimit(entity.getCustomFieldLimit());
        data.setRecordFlag(entity.getRecordFlag());
        data.setCreatedDate(entity.getCreatedDate());
        data.setCreatedId(entity.getCreatedId());
        data.setUpdatedDate(entity.getUpdatedDate());
        data.setUpdatedId(entity.getUpdatedId());

        List<TicketConfigTemplateVO.FieldInfo> defaultFieldList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getDefaultFields())) {
            List<TicketTemplate.FieldInfo> defaultFields = entity.getDefaultFields();
            for (TicketTemplate.FieldInfo fieldInfo : defaultFields) {
                defaultFieldList.add(TicketConfigTemplateVO.FieldInfo.getInstance(fieldInfo));
            }
        }
        data.setDefaultFields(defaultFieldList);

        List<TicketConfigTemplateVO.FieldInfo> customFieldList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getDefaultFields())) {
            List<TicketTemplate.FieldInfo> customFields = entity.getCustomFields();
            for (TicketTemplate.FieldInfo fieldInfo : customFields) {
                customFieldList.add(TicketConfigTemplateVO.FieldInfo.getInstance(fieldInfo));
            }
        }
        data.setCustomFields(customFieldList);

        return data;
    }


    @Nullable
    public static TicketConfigRuleVO entityToTicketConfigRuleVO(TicketRule entity) {
        if (Objects.isNull(entity)) {
            return null;
        }
        TicketConfigRuleVO data = new TicketConfigRuleVO();
        data.setId(entity.getId());
        data.setRecordFlag(entity.getRecordFlag());
        data.setCreatedId(entity.getCreatedId());
        data.setCreatedDate(entity.getCreatedDate());
        data.setUpdatedId(entity.getUpdatedId());
        data.setUpdatedDate(entity.getUpdatedDate());

        data.setSurveyId(entity.getSurveyId());
        data.setTicketConfigId(entity.getTicketConfigId());
        data.setTitle(entity.getTitle());
        data.setGid(entity.getGid());
        data.setOperation(entity.getOperation());
        data.setCreator(entity.getCreator());
        data.setVersion(entity.getVersion());
        data.setType(entity.getType());
        data.setRecordFlag(entity.getRecordFlag());
        data.setCreatedDate(entity.getCreatedDate());
        data.setCreatedId(entity.getCreatedId());
        data.setUpdatedDate(entity.getUpdatedDate());
        data.setUpdatedId(entity.getUpdatedId());
        data.setCondition(TicketConfigRuleVO.Condition.getInstance(entity.getCondition()));

        List<TicketConfigRuleVO.Target> targetList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getTargetList())) {
            List<TicketRule.Target> targets = entity.getTargetList();
            for (TicketRule.Target target : targets) {
                targetList.add(TicketConfigRuleVO.Target.getInstance(target));
            }
        }
        data.setTargetList(targetList);

        List<TicketConfigRuleVO.Condition> conditionList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getConditionList())) {
            List<TicketRule.Condition> conditions = entity.getConditionList();
            for (TicketRule.Condition condition : conditions) {
                conditionList.add(TicketConfigRuleVO.Condition.getInstance(condition));
            }
        }
        data.setConditionList(conditionList);

        return data;
    }

    @Nullable
    public static TicketNotifyConfigVO entityToTicketNotifyConfigVO(TicketNotifyConfig entity) {
        if (Objects.isNull(entity)) {
            return null;
        }
        TicketNotifyConfigVO data = new TicketNotifyConfigVO();
        data.setId(entity.getId());
        data.setRecordFlag(entity.getRecordFlag());
        data.setCreatedId(entity.getCreatedId());
        data.setCreatedDate(entity.getCreatedDate());
        data.setUpdatedId(entity.getUpdatedId());
        data.setUpdatedDate(entity.getUpdatedDate());

        data.setOrgId(entity.getOrgId());
        data.setTicketConfigId(entity.getTicketConfigId());
        data.setInstant(entity.getInstant());
        data.setStatus(entity.getStatus());
        data.setMode(entity.getMode());
        data.setPeriod(entity.getPeriod());
        data.setEvent(entity.getEvent());
        data.setHoursLimit(entity.getHoursLimit());
        data.setHasReceiver(entity.getHasReceiver());
        data.setHasCcList(entity.getHasCcList());
        data.setSmtpId(entity.getSmtpId());
        data.setCustomAttr(TicketNotifyConfigVO.CustomAttr.getInstance(entity.getCustomAttr()));
        data.setWays(NotifySettingWayVO.getInstance(entity.getWays()));

        List<TicketNotifyConfigVO.ReceiveUser> receiveUserList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getReceiverList())) {
            List<TicketNotifyConfig.ReceiveUser> receivers = entity.getReceiverList();
            for (TicketNotifyConfig.ReceiveUser receiver : receivers) {
                receiveUserList.add(TicketNotifyConfigVO.ReceiveUser.getInstance(receiver));
            }
        }
        data.setReceivers(receiveUserList);

        List<TicketNotifyConfigVO.RoleRegion> ccVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entity.getCcList())) {
            List<TicketNotifyConfig.RoleRegion> ccList = entity.getCcList();
            for (TicketNotifyConfig.RoleRegion cc : ccList) {
                ccVoList.add(TicketNotifyConfigVO.RoleRegion.getInstance(cc));
            }
        }
        data.setCcList(ccVoList);

        // 消息库新增模板相关字段
        data.setEmailContent(entity.getEmailContent());
        data.setEmailSubject(entity.getEmailSubject());
        data.setEmailTemplateId(entity.getEmailTemplateId());
        data.setSmsContent(entity.getSmsContent());
        data.setSmsSubject(entity.getSmsSubject());
        data.setSmsTemplateId(entity.getSmsTemplateId());

        return data;
    }
}
